<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动名称" prop="approvalName">
        <el-input v-model="queryParams.approvalName" placeholder="请输入活动名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="任务性质" prop="approvalTask">
        <el-input v-model="queryParams.approvalTask" placeholder="请输入任务性质" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['manage:approval:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['manage:approval:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['manage:approval:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['manage:approval:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="approvalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center" prop="id" />
      <el-table-column label="活动名称" align="center" prop="approvalName" />
      <el-table-column label="任务性质" align="center" prop="approvalTask" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ scope.row.startTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主体类型" align="center" prop="applyType">
        <template #default="scope">
          <dict-tag :options="apply_type" :value="scope.row.applyType" />
        </template>
      </el-table-column>
      <el-table-column label="单位/个人名称" align="center" prop="applyName" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="审批状态" align="center" prop="approvalStatus">
        <template #default="scope">
          <dict-tag :options="approval_status" :value="scope.row.approvalStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:approval:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['manage:approval:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改申请单对话框 -->
    <el-dialog :title="title" v-model="open" width="900px" append-to-body>
      <el-form ref="approvalRef" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="主体类型" prop="applyType">
              <el-select v-model="form.applyType" placeholder="请选择主体类型">
                <el-option v-for="dict in apply_type" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="名称" prop="applyName">
              <el-select v-model="form.applyName" placeholder="请选择名称" clearable filterable allow-create
                default-first-option reserve-keyword>
                <el-option v-for="item in apply_list" :key="item.id" :label="item.applyName" :value="item.applyName">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="活动名称" prop="approvalName">
              <el-input v-model="form.approvalName" placeholder="请输入活动名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="操控模式" prop="approvalMode">
              <el-select v-model="form.approvalMode" placeholder="请选择操控模式">
                <el-option v-for="dict in approval_mode" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最大高度" prop="maxHeight">
              <el-input v-model="form.maxHeight" placeholder="请输入最大飞行高度" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最大速度" prop="maxSpeed">
              <el-input v-model="form.maxSpeed" placeholder="请输入最大飞行速度" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="飞行模式" prop="flyMode">
              <el-select v-model="form.flyMode" placeholder="请选择飞行模式">
                <el-option v-for="dict in fly_mode" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任务性质" prop="approvalTask">
              <el-input v-model="form.approvalTask" placeholder="请输入任务性质" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker clearable v-model="form.startTime" type="date" value-format="YYYY-MM-DD"
                placeholder="请选择开始时间" style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker clearable v-model="form.endTime" type="date" value-format="YYYY-MM-DD"
                placeholder="请选择结束时间" style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人" prop="contactPerson">
              <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item prop="netFlag">
              <el-checkbox v-model="form.netFlag" label="中继飞行"></el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item prop="weightFlag">
              <el-checkbox v-model="form.weightFlag" label="运载物品"></el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item prop="publicFlag">
              <el-checkbox v-model="form.publicFlag" label="公共场所"></el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item prop="relateFlag">
              <el-checkbox v-model="form.relateFlag" label="不涉及"></el-checkbox>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="飞行保障" prop="flySafety">
              <el-input v-model="form.flySafety" type="textarea" placeholder="请输入飞行保障" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备降场地" prop="landSite">
              <el-input v-model="form.landSite" type="textarea" placeholder="请输入备降场地" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="批准文件" prop="approvalFiles">
              <el-upload v-model:file-list="approvalFilesList" class="upload-demo" action="/upload" :auto-upload="false"
                :on-change="handleApprovalFilesChange" :on-remove="handleApprovalFilesRemove">
                <el-button type="primary">选择文件</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他文件" prop="approvalOthers">
              <el-upload v-model:file-list="approvalOthersList" class="upload-demo" action="/upload"
                :auto-upload="false" :on-change="handleApprovalOthersChange" :on-remove="handleApprovalOthersRemove">
                <el-button type="primary">选择文件</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="申请区域" prop="approvalZone">
              <el-input v-model="form.approvalZone" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Approval">
import { listApproval, getApproval, delApproval, addApproval, updateApproval } from "@/api/manage/approval"
import { listApply } from "@/api/manage/apply"
import { loadAllParams } from "@/api/page";

const { proxy } = getCurrentInstance()
const { approval_mode, fly_mode, approval_status, apply_type } = proxy.useDict('approval_mode', 'fly_mode', 'approval_status', 'apply_type')

const approvalList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    applyId: null,
    applyType: null,
    applyName: null,
    approvalName: null,
    approvalMode: null,
    flyMode: null,
    approvalTask: null,
    startTime: null,
    endTime: null,
    contactPerson: null,
    contactPhone: null,
  },
  rules: {
    applyName: [
      { required: true, message: "名称不能为空", trigger: "blur" }
    ],
    approvalName: [
      { required: true, message: "活动名称不能为空", trigger: "blur" }
    ],
    approvalMode: [
      { required: true, message: "操控模式不能为空", trigger: "change" }
    ],
    flyMode: [
      { required: true, message: "飞行模式不能为空", trigger: "change" }
    ],
    approvalTask: [
      { required: true, message: "任务性质不能为空", trigger: "blur" }
    ],
    maxHeight: [
      { required: true, message: "最大飞行高度不能为空", trigger: "blur" }
    ],
    maxSpeed: [
      { required: true, message: "最大飞行速度不能为空", trigger: "blur" }
    ],
    startTime: [
      { required: true, message: "开始时间不能为空", trigger: "blur" }
    ],
    endTime: [
      { required: true, message: "结束时间不能为空", trigger: "blur" }
    ],
    contactPerson: [
      { required: true, message: "联系人不能为空", trigger: "blur" }
    ],
    contactPhone: [
      { required: true, message: "联系电话不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询申请单列表 */
function getList() {
  loading.value = true
  listApproval(queryParams.value).then(response => {
    approvalList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    applyId: null,
    applyType: null,
    applyName: null,
    approvalName: null,
    approvalMode: null,
    flyMode: null,
    approvalTask: null,
    maxHeight: null,
    maxSpeed: null,
    startTime: null,
    endTime: null,
    contactPerson: null,
    contactPhone: null,
    netFlag: null,
    weightFlag: null,
    publicFlag: null,
    relateFlag: null,
    flySafety: null,
    landSite: null,
    approvalFiles: null,
    approvalOthers: null,
    approvalZone: null,
    zoneFiles: null,
    approvalStatus: null,
    applyTime: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null
  }
  proxy.resetForm("approvalRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加申请单"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getApproval(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改申请单"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["approvalRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateApproval(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addApproval(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除申请单编号为"' + _ids + '"的数据项？').then(function () {
    return delApproval(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/approval/export', {
    ...queryParams.value
  }, `approval_${new Date().getTime()}.xlsx`)
}

/* 查询申请主体列表  */
const apply_list = ref([]);
/**
 * 获取申请列表数据
 * @description 调用listApply接口获取申请列表，并将返回的数据赋值给applyList响应式变量
 * @param {Object} loadAllParams - 请求参数对象，包含查询条件等参数
 * @returns {void}
 */
function getApplyList() {
  listApply(loadAllParams).then(response => {
    apply_list.value = response.rows;
  });
}

getApplyList();

getList()
</script>
